package com.jiesen.resource.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

@org.springframework.context.annotation.Configuration
public class QiniuUtil {

    //...生成上传凭证，然后准备上传
    private static String accessKey;
    private static String secretKey;
    private static String bucket;

    //上传地址前缀
    private static String urlPrefix;

    @Value("${qiniu.bucket}")
    public void setBucket(String bucket) {
        QiniuUtil.bucket = bucket;
    }

    @Value("${qiniu.access-key}")
    public void setAccessKey(String accessKey) {
        QiniuUtil.accessKey = accessKey;
    }

    @Value("${qiniu.secret-key}")
    public void setSecretKey(String secretKey) {
        QiniuUtil.secretKey = secretKey;
    }

    @Value("${qiniu.urlPrefix}")
    public void setUrlPrefix(String urlPrefix) {
        QiniuUtil.urlPrefix = urlPrefix;
    }

    public static String getUrlPrefix() {
        return urlPrefix;
    }

    public static String getSecretKey() {
        return secretKey;
    }

    public static String getBucket() {
        return bucket;
    }

    public static String getAccessKey() {
        return accessKey;
    }

    public static String getToken(){
        Auth auth = Auth.create(accessKey, secretKey);
        return  auth.uploadToken(bucket);
    }

    //文件上传至七牛云
    public static int fileUploadToQiniu(MultipartFile file,String name){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(file.getInputStream(), name, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            //System.out.println(putRet.key);
            // System.out.println(putRet.hash);
        } catch (Exception ex) {
            throw new RuntimeException("上传七牛云失败");
            /*Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }*/
        }

        return 1;
    }

    //移动或重命名文件
    public static int editNameToQiniu(String key ,String name){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.move(bucket, key, bucket, name);
        } catch (QiniuException ex) {
            //如果遇到异常，说明移动失败
            //System.err.println(ex.code());
            //System.err.println(ex.response.toString());
        }
        return 1;
    }

    //删除七牛云文件
    public static int fileDeleteToQiniu(String key){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            throw new RuntimeException("删除七牛云文件失败");
            //如果遇到异常，说明删除失败
            //System.err.println(ex.code());
            //System.err.println(ex.response.toString());
        }
        return 1;
    }
}
